package com.roin.learn.mybatisplus.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * mybatis-plus元对象处理器
 * 用于处理使用了@TableField(fill = FieldFill.XXXX)的字段，在更新和插入时自动填充数据
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
//        this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now());
//        this.strictUpdateFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());

        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {

//        this.strictUpdateFill(metaObject,"updateTime", LocalDateTime.class,LocalDateTime.now());

        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
    }
}
